<template>
  <div class="common-wrap">
    <QueryForm
      ref="conditionForm"
      module="sailingSchedule"
      :searchBtnLoading="loading"
      v-model:searchForm="searchForm"
      v-model:search-column="searchColumn"
      @handle-search="onSearch"
    />
    <div class="common-box">
      <el-row style="height: 40px" class="con-button-rows">
        <ButtonGroup
          :disabledDelete="multipleSelection.length === 0"
          :hasPermiAdd="['oceanFreight:sailingSchedule:add']"
          :hasPermiDelete="['oceanFreight:sailingSchedule:delete']"
          :buttonType="['add', 'delete']"
          @add="onAdd"
          @edit="onEdit"
          @delete="onDelete"
        >
        </ButtonGroup>
      </el-row>
      <MyTable
        tableId="sailingSchedule"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pageVo="pageVo"
        ref="commonTable"
        moduleName="sailingSchedule"
        @handleSelectionChange="handleSelectionChange"
        @handle-page-change="handlePageChange"
      ></MyTable>
    </div>
  </div>
</template>

<script setup lang='tsx'>
import { ref, reactive, onMounted, onUnmounted } from "vue";
import { useI18n } from "vue-i18n";
import * as api from "@/api/index.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { queryFormColumnFilters, hasPermi } from "@/utils/mUtils.js";
import { useRouter } from "vue-router";
const router = useRouter();
let { t } = useI18n();

defineOptions({
  name: "SailingSchedule",
});
const pageVo = reactive({
  pageSize: 50,
  total: 10,
  currentPage: 1,
  status: true,
});
const loading = ref(true);
const dataSource = ref([]);
const columns = ref([
  {
    type: "checkbox",
    width: 50,
    align: "center",
    fixed: "left",
  },
  {
    title: "序号",
    type: "seq",
    width: 60,
    fixed: "left",
  },
  {
    field: "scheduleNo",
    title: "船期编号",
    slots: {
      default: ({ row }) => {
        return [
          <el-button
            type="primary"
            disabled={!hasPermi(["oceanFreight:sailingSchedule:edit"])}
            link
            onClick={() => {
              onEdit(row);
            }}
          >
            {row.scheduleNo || ""}
          </el-button>,
        ];
      },
    },
  },
  {
    field: "estimatedStuffingTime",
    title: "预计装柜时间",
  },
  {
    field: "month",
    title: "月",
  },
  {
    field: "week",
    title: "周",
  },
  {
    field: "lineName",
    title: "干线名称",
  },
  {
    field: "lineDetailCode",
    title: "航线代码",
  },
  {
    field: "providerName",
    title: "船公司",
  },

  {
    field: "vesselName",
    title: "船名",
  },
  {
    field: "voyageNo",
    title: "航次",
  },
  {
    field: "loadingPortName",
    title: "起运港",
  },
  {
    field: "estimatedTimeDeparture",
    title: "ETD",
  },
  {
    field: "actualTimeDeparture",
    title: "ATD",
  },
  {
    field: "dischargePortName",
    title: "卸货港",
  },
  {
    field: "estimatedTimeArrivalDischarge",
    title: "ETA",
  },
  {
    field: "actualTimeArrivalDischarge",
    title: "ATA",
  },
  {
    field: "deliveryPortName",
    title: "目的港",
  },
  {
    field: "estimatedTimeArrivalDelivery",
    title: "ETA",
  },
  {
    field: "actualTimeArrivalDelivery",
    title: "ATA",
  },
  {
    field: "proposedTransitTime",
    title: "预期航程",
  },
  {
    field: "portOpeningTime",
    title: "开港时间",
  },
  {
    field: "shippingInstructionCutoffTime",
    title: "截单时间",
  },
  {
    field: "containerYardClosingTime",
    title: "截重时间",
  },
  {
    field: "customsClosingTime",
    title: "截关时间",
  },
  {
    field: "cargoReceivingCutoffTime",
    title: "截货时间",
  },
  {
    field: "verifiedGrossMassCutoffTime",
    title: "截VGM时间",
  },
  {
    field: "vesselSchedule",
    title: "船期描述",
  },
  {
    field: "createByName",
    title: "创建人",
  },
  {
    field: "createTime",
    title: "创建时间",
  },
]);
const searchForm = ref({});
const searchColumn = ref([
  {
    type: "label_input",
    label: "船期编号",
    prop: "scheduleNo",
  },
  {
    type: "remote_select",
    label: "干线名称",
    prop: "lineDetailId",
    params: {
      searchKey: "searchKey",
      keyValue: "id",
      name: "lineName",
      abbreviation: "code",
      showCustomizedOption: true,
      displayKey: {
        customizeLeft: "providerName",
        customizeCenter: "loadingPortName",
        customizeRight: "deliveryPortName",
      },
      queryId: true,
    },
    apiModule: api.default.common.trunkLine.queryLineDetailList,
    placeholder: "请选择干线名称",
  },
  {
    label: "船名",
    prop: "vesselName",
    type: "label_input_autocomplete",
    placeholder: "请输入船名",
    paramsKey: "vesselName",
    value: "vesselName",
    apiModule:
      api.default.oceanFreight.sailingSchedule.queryHistoryVesselNameList,
  },
  {
    type: "remote_select",
    label: "船公司",
    prop: "providerCode",
    params: {
      searchKey: "providerNameZh",
      keyValue: "providerCode",
      name: "providerNameZh",
      showCustomizedOption: true,
      abbreviation: "providerAbbr",
      displayKey: {
        customizeLeft: "providerCode",
        customizeRight: "customizeCenter",
      },
    },
    apiModule: api.default.common.serviceProvider.list,
    placeholder: "请选择船公司",
  },
  {
    type: "label_select",
    label: "起运港",
    prop: "loadingPortCode",
    labelVal: "portCode",
    labelName: "portName",
  },
  {
    type: "label_select",
    label: "目的港",
    prop: "deliveryPortCode",
    labelVal: "portCode",
    labelName: "portName",
  },
  {
    type: "label_select",
    label: "卸货港",
    prop: "dischargePortCode",
    labelVal: "portCode",
    labelName: "portName",
  },
  {
    type: "label_date_picker",
    label: "ETD区间",
    prop: "etdStart",
    dateType: "datetimerange",
  },
]);
const multipleSelection = ref([]);
onMounted(() => {
  onSearch();
  portList();
});
const portList = async () => {
  const { rows } = await api.default.common.port.list({
    pageSize: 300,
  });
  queryFormColumnFilters(searchColumn.value, "loadingPortCode").options = rows;
  queryFormColumnFilters(searchColumn.value, "deliveryPortCode").options = rows;
  queryFormColumnFilters(searchColumn.value, "dischargePortCode").options =
    rows;
};
const list = () => {
  loading.value = true;
  const params = {
    ...searchForm.value,
  };
  if (params?.etdStart) {
    [params.etdStart, params.etdEnd] = params.etdStart;
  }
  api.default.oceanFreight.sailingSchedule
    .list({
      ...params,
      pageNum: pageVo.currentPage,
      pageSize: pageVo.pageSize,
    })
    .then((res) => {
      dataSource.value = res.rows;
      loading.value = false;
      pageVo.total = res.total;
    })
    .catch(() => {
      loading.value = false;
    });
};

const onAdd = () => {
  router.push({
    path: "/oceanFreight/sailing/sailingScheduleAdd",
    query: {
      title: "船期新增",
    },
  });
};
const onEdit = (row) => {
  router.push({
    path: "/oceanFreight/sailing/sailingScheduleAdd",
    query: {
      title: "船期修改",
      id: row.id,
      detailed: row.scheduleNo,
    },
  });
};
const onDelete = () => {
  const ids = multipleSelection.value.map((item) => item.id);
  ElMessageBox.confirm(t("是否确定删除当前选择数据？"), t("提示"), {
    confirmButtonText: t("确定"),
    cancelButtonText: t("取消"),
    type: "warning",
  })
    .then(() => {
      api.default.oceanFreight.sailingSchedule.remove(ids).then(() => {
        ElMessage.success(t("删除成功"));
        onSearch();
      });
    })
    .catch(() => {});
};
const handleSelectionChange = (list) => {
  multipleSelection.value = list;
};
const handlePageChange = (val) => {
  const { currentPage, pageSize } = val;
  pageVo.currentPage = currentPage;
  pageVo.pageSize = pageSize;
  list();
};
const onSearch = () => {
  pageVo.currentPage = 1;
  list();
};
</script>

<style lang="scss" scoped>
.pcMenu {
  height: 100%;
  width: 100%;
  background: #fff;
}
</style>